package com.bw.module_home.ui

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.module_home.R
import com.bw.module_home.databinding.ActivityTenantBinding
import com.bw.module_home.property.adapter.TenantAdapter
import com.bw.module_home.property.model.TenantEntityItem
import com.bw.module_home.property.viewmodel.PropertyViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/property/tenant")
class TenantActivity : BaseActivity<PropertyViewModel, ActivityTenantBinding>() {
    private val tenantAdapter = TenantAdapter()
    private val tenantList: MutableList<TenantEntityItem> = mutableListOf()
    private val vm: PropertyViewModel by viewModels()

    override val mLayoutResId: Int
        get() = R.layout.activity_tenant

    override fun initView() {
        super.initView()
        vm.getTenantList()
        mBinding.tenantBack.setOnClickListener {
            finish()
        }
        mBinding.tenantRv.adapter = tenantAdapter
        mBinding.tenantRv.layoutManager = LinearLayoutManager(this)
        mBinding.tenantSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                tenantAdapter.data.clear()
                val filter = tenantList.filter {
                    it.enterpriseName.contains(s.toString())
                }
                tenantAdapter.data.addAll(filter)
                tenantAdapter.notifyDataSetChanged()
            }

            override fun afterTextChanged(s: Editable?) {
                if (s?.length == 0) {
                    vm.getTenantList()
                }
            }
        })
    }

    override fun initListener() {
        super.initListener()
        vm.getTenantSuccess.observe(this) {
            Log.d("TAG", "initDatattt: ${it.size}")
            tenantList.clear()
            tenantAdapter.data.clear()
            tenantList.addAll(it)
            tenantAdapter.data.addAll(it)
            tenantAdapter.notifyDataSetChanged()
        }
    }
}